﻿var popupStatus = 0;
var windowWidth = document.documentElement.clientWidth;
var windowHeight = document.documentElement.clientHeight;
var openWinTime ;
function loadPopup(bg){	
	if(popupStatus==0){
		if(bg) $("#backgroundPopup").stop(false,false).animate({"opacity":0.65,"top":0,"left":0,"height": $(document).height()},'normal');
		//$("#popupContact").show();
		popupStatus = 1;
	}	
}

function disablePopup(onClose){
	var top = parseInt($("#popupContact").css('top'));
	if(popupStatus==1){
		$("#popupContact").animate({'top': top - 50, 'opacity':'hide'},'normal');//'left': -windowWidth / 2
		popupStatus = 0;
	}
	$("#backgroundPopup").animate({ 'opacity':'hide' },'normal');
	if(onClose) setTimeout(onClose,10);
}

function centerPopup(bg){
	var popupHeight = $("#popupContact").height();
	var popupWidth = $("#popupContact").width();
	var myWidth = 0, myHeight = 500;
	if (typeof (window.innerWidth) == "number") {
			myWidth = window.innerWidth;
			myHeight = window.innerHeight;
		} else {
			if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
				myWidth = document.documentElement.clientWidth;
				myHeight = document.documentElement.clientHeight;
			} else {
				if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
					myWidth = document.body.clientWidth;
					myHeight = document.body.clientHeight;
			}
		}
	}			 
	var left =( myWidth - popupWidth) / 2 ;
	var top = $(window).scrollTop() + ( myHeight - popupHeight ) / 2  - 50;
	
	$("#popupContact").css({"top": top -50,"left":left,'display':'none'}).animate({"top": top,"left":left,'opacity':'show'},'normal');
	if(bg) $("#backgroundPopup").css({ "height": $('html').height() ,'display':'none'}).animate({'opacity':'show'},'normal');	
}

$.openWin = function (option){
	var opts = $.extend({}, $.openWin.defaults, option);

	var openWinout = " " +
		'<div id="popupContact">' +
			'<i class="status '+ opts.status +'"></i><i class="showMsg">'+ opts.msg +'</i><i class="bottom"></i>' +
		'</div><div id="backgroundPopup"></div>' ;
	if( !$('#popupContact').get(0) ) $('body').append(openWinout);
	else{
		$('#popupContact > i.showMsg').html(opts.msg);
		$('#popupContact > i.status').removeClass().addClass('status').addClass(opts.status);
	}
	$('#popupContact').unbind('click').click(function(){
		if(opts.canClose) disablePopup(opts.onClose);
	});
	if(!opts.bg) disablePopup();	
	$('#popupContact > .ShowMsg').html(opts.msg);
	centerPopup(opts.bg);	
	loadPopup(opts.bg);
	if( openWinTime ) clearTimeout(openWinTime);
	if( opts.time ) openWinTime = setTimeout("disablePopup()" , opts.time );	
}
$.openWin.defaults = {time : null,bg : true,status : 'success', canClose: true ,onClose : function(){}};

$(function(){ 
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){ disablePopup();}
	});
});